﻿using System;
using System.Collections.Generic;
using System.Text;
using SysCommon.Business.CVSS.WS.Stat;
using SysCommon.Entity.CVSS.WS.Stat;
using SysCommon.Tools;
using System.Windows.Forms;
using dotnetCHARTING.WinForms;
using SysCommon;
using System.Drawing;

namespace CVSS.Client.SC.UI
{
    public class PeccancyStakeOutMDT
    {
        private PeccancyStakeOut frm;
        private WS_Stat_BizCtrlImpl WS_Stat;
        private Chart chart;

        public bool ShowForm()
        {
            try
            {
                WS_Stat = new WS_Stat_BizCtrlImpl();
                WS_Stat.StatistcByCarPlateCompleted += new SysCommon.Events.CVSS.WS.Stat.StatistcByCarPlateCompletedEventHandler(WS_Stat_StatistcByCarPlateCompleted);
                frm = new PeccancyStakeOut();
                chart = new Chart();

                frm.ButtStat.Click += new EventHandler(ButtStat_Click);


                chart = new Chart();
                SetChartStyle(ref chart);//设置样式



                frm.ShowDialog();
                return true;

            }
            catch (Exception ex)
            {
                SysCommon.Global.mainLog.Error("调用showForm出错", ex);
                return false;
            }
        }

        void ButtStat_Click(object sender, EventArgs e)
        {
            if (frm.TCarPlate.Text.Trim().Length == 0)
            {
                MessageBox.Show("请输入车牌号码", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                frm.TCarPlate.Focus();
                return;
            }

            if (frm.StartTime.datetime > frm.EndTime.datetime)
            {
                MessageBox.Show("开始日期大于结束日期", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                frm.StartTime.Reset();
                frm.EndTime.Reset();
                return;
            }

            if (WS_Stat == null) return;

            StatistCarByPlateCondition_Ety statcondition = new StatistCarByPlateCondition_Ety();
            statcondition.CARPLATE = frm.TCarPlate.Text.Trim();
            statcondition.BEGINTIME = frm.StartTime.datetime;
            statcondition.ENDTIME = frm.EndTime.datetime;

            chart.SeriesCollection.Clear();
            SetChartStyle(ref chart);
            chart.RefreshChart();

            frm.ButtStat.Enabled = false;

            frm.panelChart.Controls.Remove(chart);

            frm.picwait.Visible = true;
            Application.DoEvents();

            WS_Stat.StatistcByCarPlateAsync(statcondition);

        }

        void WS_Stat_StatistcByCarPlateCompleted(object sender, SysCommon.Events.CVSS.WS.Stat.StatistcByCarPlateCompletedEventArgs e)
        {
            try
            {
                if (e.Result == null || e.Result.Length == 0)
                {
                    frm.ButtStat.Enabled = true;
                    frm.picwait.Visible = false;
                    MessageBox.Show("未找到相关数据!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                StatistCarByPlate_Ety[] etys = e.Result as StatistCarByPlate_Ety[];


                SeriesCollection SC = new SeriesCollection();
                foreach (StatistCarByPlate_Ety ety in etys)
                {
                    Series s = new Series();
                    s.Name = ety.PECCANCYTYPENAME;
                    Element ele = new Element();
                    ele.Name = "Element " + ety.PECCANCYTYPENAME;
                    ele.YValue = Convert.ToDouble(ety.ALLCOUNT);
                    s.Elements.Add(ele);
                    SC.Add(s);
                }


                if (SC == null) return;
                if (chart == null) return;


                chart.Title = frm.TCarPlate.Text + chart.Title;
                chart.SeriesCollection.Add(SC);

                frm.panelChart.Controls.Add(chart);

                chart.RefreshChart();

                //Application.DoEvents();

                frm.picwait.Visible = false;
                frm.ButtStat.Enabled = true;

            }
            catch (Exception ex)
            {
                Global.mainLog.Error("调用 WS_Stat_StatistcByCarPlateCompleted 出错", ex);
                frm.picwait.Visible = false;
                frm.ButtStat.Enabled = true;
            }
        }

        private void SetChartStyle(ref Chart chart)
        {
            chart.Title = "违法监控分析图";
            chart.Dock = DockStyle.Fill;
            chart.ChartArea.Background.Color = Color.FromArgb(215, 243, 250);
            chart.Use3D = true;
            chart.Type = ChartType.Pie;
            chart.DefaultSeries.DefaultElement.Transparency = 0;
            chart.DefaultSeries.DefaultElement.ShowValue = true;
            chart.PieLabelMode = PieLabelMode.Outside;
            chart.TempDirectory = "temp";
            chart.MarginLeft = 280;
            chart.MarginRight = 150;
            chart.Debug = true;
        }
    }
}
